package com.lyhcc.software;

import com.lyhcc.utils.Calculator;
import com.lyhcc.utils.Fraction;
import com.lyhcc.utils.StackCal;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        finshWorking();

    }

    private static void finshWorking() {
        MainCal mainCal = new MainCal(1000,1000,2,10,50);
        ArrayList<String> list = mainCal.formularBorn();
        FileOutputStream fos = null;
        ArrayList<Fraction>res = mainCal.formularAnswer(list);


        writeInFile(list, fos,"question_");

        writeInFile(res, fos,"answer_");
    }


    private static void writeInFile(ArrayList list, FileOutputStream fos,String pathname) {
        try{
            fos = new FileOutputStream(new File("src/com/lyhcc/"+pathname+splicingFileName()+".txt"));
            int cnt  = 0;
            for (Object s : list) {
                cnt++;

                if(s instanceof Fraction){
                    Fraction fraction = (Fraction)s;
                    if(fraction.getDenominator()==1) {
                        String str = Integer.toString(fraction.getMolecular());
                        fos.write((cnt+". " + str +"\r\n").getBytes());
                    }else{
                        fos.write((cnt+". " + s +"\r\n").getBytes());
                    }
                }else {
                    fos.write((cnt+". " + s +"\r\n").getBytes());
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String splicingFileName(){
        Calendar calendar = Calendar.getInstance();
        return ""+calendar.getTimeInMillis();
    }

}
